import Vue from 'vue'
import Router from 'vue-router'

import HomePage from './index.vue'
import Shop from './../components/index.vue'
import App from './../../App.vue'
import Mall from './../mall/mall.vue'
import Personal from './../personal/personal.vue'
import Login from './../login/login.vue'
import Register from './../register/register.vue'
import ForgetPwd from './../forgetPwd/forgetPwd.vue'
import SelectAddress from './../selectAddress/selectAddress.vue'
import ProductList from './../productList/productList.vue'
import Cart from './../cart/cart.vue'
import More from './../more/more.vue'
import FreeBack from './../more/freeBack.vue'
import Address from './../address/address.vue'
import AddAddress from './../address/addAddress.vue'
import SelectBuilding from './../address/selectBuilding.vue'
import Test from './../test.vue'
import CouponList from './../couponList/couponList.vue'
import UCouponList from './../couponList/uCouponList.vue'
import OrderList from './../orderList/orderList.vue'
import OrderInfo from './../orderInfo/orderInfo.vue'
import Comment from './../comment/comment.vue'
import AccountLog from './../accountLog/accountLog.vue'
import Recharge from './../recharge/recharge.vue'
import Pay from './../pay/pay.vue'
import Balance from './../balance/balance.vue'
import UpdateUserInfo from './../personal/updateUserInfo.vue'
import changePwd from './../personal/changePwd.vue'
import useDes from './../couponList/use_des.vue'
import serviceCenter from './../more/ServiceCentre.vue'

// plugins
import Device from './../../plugins/device'
import DeviceDemo from './../../demos/Device'
import ToastPlugin from './../../plugins/toast'
import AlertPlugin from './../../plugins/alert'

const FastClick = require('fastclick')
FastClick.attach(document.body)

Vue.use(Router)
Vue.config.devtools = true

// $device
Vue.use(Device)
Vue.use(ToastPlugin)
Vue.use(AlertPlugin)

const router = new Router({
  transitionOnLoad: true
})

/**
* sync router params
*/
import { sync } from 'vuex-router-sync'
import store from './../../vuex/store'

let history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

/**
* sync router loading status
*/
const commit = store.commit || store.dispatch
router.beforeEach(({ to, from, next }) => {
  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)
  //console.log("to=" + to.path + "  from=" + from.path)
  if (toIndex) {
    if (toIndex > fromIndex || !fromIndex) {
      commit('UPDATE_DIRECTION', 'forward')
    } else {
      commit('UPDATE_DIRECTION', 'reverse')
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    commit('UPDATE_DIRECTION', 'forward')
  }
  commit('UPDATE_LOADING', true)
  setTimeout(next, 200)
})


router.afterEach(() => {
  commit('UPDATE_LOADING', false)
})

sync(store, router)


router.map({
  '/test': {
    component: Test
  },
  '/home': {
    component: HomePage
  },
  '/shop':{
    component: Shop
    //component: HomePage
  },
  '/mine':{
    component: HomePage
  },
  '/tabbar/mall': {
    component: Mall
  },
  '/tabbar/personal': {
    component: Personal,
  },
  '/login':{
    component: Login
  },
  '/register': {
    component: Register
  },
  '/forgetPwd': {
    component: ForgetPwd
  },
  '/productList/:classId/:closetime/:classImage':{
    name: "ProductList",
    component: ProductList
  },
  '/selectAddress': {
    component: SelectAddress
  },
  '/cart':{
    name:'cart',
    component:Cart
  },
  '/more': {
    component: More
  },
  '/freeBack': {
    component: FreeBack
  },
  '/addressList': {
    component: Address
  },
  '/newAddress': {
    component: AddAddress
  },
  '/selectBuilding': {
    component: SelectBuilding
  },
  '/couponList': {
    component: CouponList
  },
  '/uCouponList': {
    component: UCouponList
  },
  '/orderList': {
    component: OrderList
  },
  '/orderInfo': {
    component: OrderInfo
  },
  '/comment': {
    component: Comment
  },
  '/pay/:freePrice/:orderId/:orderNo/:orderTime/:totalPrice': {
    name: "pay",
    component: Pay
  },
  '/balance':{
    name:'balance',
    component: Balance
  },
  '/accountLog': {
    component: AccountLog
  },
  '/updateUserInfo': {
    component: UpdateUserInfo
  },
  '/modifyPwd': {
    component: changePwd
  },
  '/useDes': {
    component: useDes
  },
  '/serviceCenter': {
    component: serviceCenter
  }
})


router.on('/', {
  component: HomePage
})

// save position for demo page
let demoScrollTop = 0
function saveDemoScrollTop () {
  demoScrollTop = window.scrollY
}

router.beforeEach(function (transition) {
  if (transition.to.fullPath !== '/mall') {
    window.removeEventListener('scroll', saveDemoScrollTop, false)
  }
  if (/\/http/.test(transition.to.path)) {
    let url = transition.to.path.split('http')[1]
    window.location.href = `http${url}`
  } else {
    transition.next()
  }
})

router.afterEach(function (transition) {
  // if (transition.to.path !== '/mall') {
  //   window.scrollTo(0, 0)
  // } else {
    window.removeEventListener('scroll', saveDemoScrollTop, false)
    // if from component page
    if (demoScrollTop && /component/.test(transition.from.path)) {
      setTimeout(function () {
        window.scrollTo(0, demoScrollTop)
      }, 100)
    }
    setTimeout(function () {
      window.addEventListener('scroll', saveDemoScrollTop, false)
    }, 100)
  // }
})

router.start(App, '#app')
